//
//  SISSMapViewController+DataRequest.m
//  SISSAPP
//
//  Created by 优房网 on 2017/6/7.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMapViewController+DataRequest.h"
#import "SISSJsonRequestNetworkingManager+Map.h"
#import "SISSMapShowViewController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>


@implementation SISSMapViewController (DataRequest)


#pragma mark - 请求数据
- (void)requestData {
    
    NSInteger visibleRange = self.visibleRange == 2 ? self.visibleRange : 1;
    //    NSInteger orderAttrs = self.orderAttrs ? self.orderAttrs : 0;
    NSString *region = self.region.length > 0 ? self.region : @"不限";
    region = [region isEqualToString:@"深圳"] ? @"不限" : region;
    NSString *regionDetail = self.regionDetail.length > 0 ? self.regionDetail : @"不限";
    NSString *price = self.price ? self.price : @"";
    NSString *houseType = self.houseType.length > 0 ? self.houseType : @"不限";
    
    NSDictionary *parameters = @{
                                 @"visibleRange":@(visibleRange),
                                 @"orderAttrs":@(1),
                                 @"region":region,
                                 @"regionDetail":regionDetail,
                                 @"price":price,
                                 @"houseType":houseType,
                                 @"area":self.area ,
                                 @"direction":self.direction,
                                 @"decoration":self.decoration
                                 };
    
    [self requestRegionDataWithParameters:parameters];
    [self requestRegionDetailDataWithParameters:parameters];
}
/**
 请求大区数据
 */
- (void)requestRegionDataWithParameters:(NSDictionary *)parameters {
    
    switch (self.mapShowType) {
        case SISSMapShowTypeHouse: {
            [kJsonRequestNetworkingManager requestFilterMapHouseRegionResourceWithParameters:parameters success:^(NSArray<SISSMapAreaModel *> *houseMapRegionArr) {
                
                self.houseMapRegionArr = houseMapRegionArr;
                
                [self.mapView setZoomLevel:13];
                [self mapView:self.mapView regionDidChangeAnimated:YES];
                
            } failed:^(NSError *error) {
                
                NSLog(@"%@",error);
            }];
            
        }
            break;
            
        case SISSMapShowTypePassenger: {
            [kJsonRequestNetworkingManager requestFilterMapPassengerRegionResourceWithParameters:parameters success:^(NSArray<SISSMapAreaModel *> *passengerMapRegionArr) {
                
                self.passengerMapRegionArr = passengerMapRegionArr;
                [self.mapView setZoomLevel:13];
                [self mapView:self.mapView regionDidChangeAnimated:YES];
                
            } failed:^(NSError *error) {
                
                NSLog(@"%@",error);
            }];
        }
            break;
            
        case SISSMapShowTypeRentOut: {
            [kJsonRequestNetworkingManager requestFilterMapRentOutRegionResourceWithParameters:parameters success:^(NSArray<SISSMapAreaModel *> *rentOutMapRegionArr) {
                
                self.rentOutMapRegionArr = rentOutMapRegionArr;
                [self.mapView setZoomLevel:13];
                [self mapView:self.mapView regionDidChangeAnimated:YES];
                
            } failed:^(NSError *error) {
                
                NSLog(@"%@",error);
            }];
        }
            break;
            
        case SISSMapShowTypeTenant: {
            [kJsonRequestNetworkingManager requestFilterMapTenantRegionResourceWithParameters:parameters success:^(NSArray<SISSMapAreaModel *> *tenantMapRegionArr) {
                
                self.tenantMapRegionArr = tenantMapRegionArr;
                [self.mapView setZoomLevel:13];
                [self mapView:self.mapView regionDidChangeAnimated:YES];
                
            } failed:^(NSError *error) {
                
                NSLog(@"%@",error);
            }];
        }
            break;
            
        default:
            break;
    }
}

/**
 请求小区（街道办）数据
 */
- (void)requestRegionDetailDataWithParameters:(NSDictionary *)parameters {
    
    
    switch (self.mapShowType) {
        case SISSMapShowTypeHouse: {
            [kJsonRequestNetworkingManager requestFilterMapHouseRegionDetailResourceWithParameters:parameters success:^(NSArray<SISSMapAreaModel *> *houseMapRegionDetailArr) {
                
                self.houseMapRegionDetailArr = houseMapRegionDetailArr;
                [self.mapView setZoomLevel:13];
                [self mapView:self.mapView regionDidChangeAnimated:YES];
                
            } failed:^(NSError *error) {
                
                NSLog(@"%@",error);
            }];
        }
            break;
            
        case SISSMapShowTypePassenger: {
            [kJsonRequestNetworkingManager requestFilterMapPassengerRegionDetailResourceWithParameters:parameters success:^(NSArray<SISSMapAreaModel *> *passengerMapRegionDetailArr) {
                
                self.passengerMapRegionDetailArr = passengerMapRegionDetailArr;
                [self.mapView setZoomLevel:13];
                [self mapView:self.mapView regionDidChangeAnimated:YES];
                
            } failed:^(NSError *error) {
                
                NSLog(@"%@",error);
            }];
        }
            break;
            
        case SISSMapShowTypeRentOut: {
            [kJsonRequestNetworkingManager requestFilterMapRentOutRegionDetailResourceWithParameters:parameters success:^(NSArray<SISSMapAreaModel *> *rentOutMapRegionDetailArr) {
                
                self.rentOutMapRegionDetailArr = rentOutMapRegionDetailArr;
                [self.mapView setZoomLevel:13];
                [self mapView:self.mapView regionDidChangeAnimated:YES];
                
            } failed:^(NSError *error) {
                
                NSLog(@"%@",error);
            }];
        }
            break;
            
        case SISSMapShowTypeTenant: {
            [kJsonRequestNetworkingManager requestFilterMapTenantRegionDetailResourceWithParameters:parameters success:^(NSArray<SISSMapAreaModel *> *tenantMapRegionDetailArr) {
                
                self.tenantMapRegionDetailArr = tenantMapRegionDetailArr;
                [self.mapView setZoomLevel:13];
                [self mapView:self.mapView regionDidChangeAnimated:YES];
                
            } failed:^(NSError *error) {
                
                NSLog(@"%@",error);
            }];
        }
            break;
            
        default:
            break;
    }
}

- (void)requestShowDataWithRegion:(NSString *)region regionDetail:(NSString *)regionDetail {
    
    // 非空处理
//    NSString *keyword = area ? area : @"";
    NSInteger visibleRange = self.visibleRange == 2 ? self.visibleRange : 1;
    //    NSInteger orderAttrs = self.orderAttrs ? self.orderAttrs : 0;
    region = region.length > 0 ? region : @"不限";
    
    NSString *price = self.price ? self.price : @"";
    NSString *houseType = self.houseType.length > 0 ? self.houseType : @"不限";
    
    NSDictionary *parameters = @{
                                 @"visibleRange":@(visibleRange),
//                                 @"orderAttrs":@(orderAttrs),
                                 @"region": ![region isEqualToString:@"深圳"] ? region : @"不限",
                                 @"regionDetail":regionDetail ? regionDetail : @"不限",
                                 @"price":price,
                                 @"houseType":houseType,
                                 @"area":self.area ,
                                 @"direction":self.direction,
                                 @"decoration":self.decoration
                                 };
    
    self.mapShowViewController.mapShowType = self.mapShowType;
    self.mapShowViewController.pageNo = 1;
    self.mapShowViewController.parameters = parameters;
}



@end
